package cn.darkhorse.device.iot.parser;

import cn.darkhorse.device.pojo.MqttData;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 智嵌物联
 * {"addr":"64","cmd":"upload","x":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"userId":"22222"}
 */
@Component("geIoParser")
public class GeIoParser extends BaseParser {

    // 开关量
    public static final String DI_PRE = "di";
    // 模拟量
    public static final String ADC_PRE = "adc";

    @Override
    Map<String, Object> doParse(MqttData data, String gatewayDeviceId) {
        String json = data.getData();
        JSONObject jsonObject = JSONObject.parse(json);

        // Step.1 获取设备点表
        Set<String> pointIds = getPointIds(gatewayDeviceId);

        // Step.1 解析
        Map<String, Object> pointValueMap = new HashMap<>();
        JSONArray x = (JSONArray) jsonObject.get("x");
        JSONArray adc = (JSONArray) jsonObject.get("adc");

        Pattern pattern = Pattern.compile("\\d");

        pointIds.forEach(pointId-> {
            if (pointId.toLowerCase().contains(DI_PRE)) {
                Matcher matcher = pattern.matcher(pointId);
                while (matcher.find()){
                    String value = x.get(Integer.parseInt(matcher.group())).toString();
                    pointValueMap.put(pointId, value);
                }
            }

            if (pointId.toLowerCase().contains(ADC_PRE)) {
                Matcher matcher = pattern.matcher(pointId);
                while (matcher.find()){
                    String value = adc.get(Integer.parseInt(matcher.group())).toString();
                    pointValueMap.put(pointId, value);
                }
            }
        });

//        addToCache(gatewayDeviceId, pointValueMap);
        return pointValueMap;
    }

    @Override
    Map<String, Object> doParse(MqttData data, String gatewayDeviceId, Set<String> pointIds) {
        String json = data.getData();
        JSONObject jsonObject = JSONObject.parse(json);

        // Step.1 获取设备点表
//        Set<String> pointIds = getPointIds(gatewayDeviceId);

        // Step.1 解析
        Map<String, Object> pointValueMap = new HashMap<>();
        JSONArray x = (JSONArray) jsonObject.get("x");
        JSONArray adc = (JSONArray) jsonObject.get("adc");

        Pattern pattern = Pattern.compile("\\d");

        pointIds.forEach(pointId-> {
            if (pointId.toLowerCase().contains(DI_PRE)) {
                Matcher matcher = pattern.matcher(pointId);
                while (matcher.find()){
                    String value = x.get(Integer.parseInt(matcher.group())).toString();
                    pointValueMap.put(pointId, value);
                }
            }

            if (pointId.toLowerCase().contains(ADC_PRE)) {
                Matcher matcher = pattern.matcher(pointId);
                while (matcher.find()){
                    String value = adc.get(Integer.parseInt(matcher.group())).toString();
                    pointValueMap.put(pointId, value);
                }
            }
        });

//        addToCache(gatewayDeviceId, pointValueMap);
        return pointValueMap;
    }


    @Override
    public LocalDateTime getCreatedAd(MqttData data) {
        return data.getAddDate();
    }

    public static void main(String[] args) {
        String json = "{\"addr\":\"64\",\"cmd\":\"upload\",\"x\":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"userId\":\"22222\"}";
        JSONObject jsonObject = JSONObject.parse(json);

        // Step.1 解析DI
        JSONArray x = (JSONArray) jsonObject.get("x");
        System.out.println(x.get(0));

        Pattern pattern = Pattern.compile("\\d");
        Matcher matcher = pattern.matcher("DI0");
        matcher.find();
        System.out.println("找到数字" + matcher.group().getClass());
    }
}
